package config;

import java.io.File;

import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class SettingConfig {

	private static final String DCCSERVER_ECHO = "dccserver-echo";

	private static final String SERVICE_REQUEST = "service-request";

	private static final String REGISTRY = "registry";

	private static final String VALUE = "value";

	private static final String KEY = "key";

	private static final String PORT = "port";

	private static final String HOST = "host";

	private static final SettingConfig SERVER_CONFIG = new SettingConfig() ;
	
	private static final String XML_CONFIG = "Config.xml" ;
	
	
	
	
	private static final SAXReader SAR_READER = new SAXReader();

	private SettingConfig() {
		super();
	}
	
	public String host() throws DocumentException {
		return this.rootElement().element(HOST).getTextTrim().trim() ;
	}
	
	public int port() throws DocumentException {
		return Integer.parseInt(this.rootElement().element(PORT).getTextTrim().trim()) ;
	}
	
	public String dccServerEcho() throws DocumentException {
		return this.rootElement().element(DCCSERVER_ECHO).element(VALUE).getTextTrim().trim() ;
	}
	
	public String regeditKey() throws DocumentException {
		return regedit().element(KEY).getTextTrim().trim() ;
	}
	
	public String regeditValue() throws DocumentException {
		return regedit().element(VALUE).getTextTrim().trim() ;
	}
	
	private Element regedit() throws DocumentException {
		return this.rootElement().element(REGISTRY) ;
	}
	
	public String requestKey() throws DocumentException {
		Element service_request = this.serviceRequest() ;
		return service_request.element(KEY).getTextTrim().trim() ;
	}
	
	public String requestValue() throws DocumentException {
		Element service_request = this.serviceRequest() ;
		return service_request.element(VALUE).getTextTrim().trim() ;
	}
	
	private final Element rootElement() throws DocumentException {
		return SAR_READER.read(new File(XML_CONFIG)).getRootElement();
	}
	
	private Element serviceRequest() throws DocumentException {
		return this.rootElement().element(SERVICE_REQUEST) ;
	}
	
	public static SettingConfig getInstance() {
		return SERVER_CONFIG ;
	}
}
